package com.itheima.controller;

import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.domain.Dish;
import com.itheima.domain.DishDTO;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
* @Author yangshuai
* @Desc:菜品管理-控制器
* @Date ${TIME} ${DATE}
* @Version 1.0
**/
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    /*
    菜品列表名称展示
     */
    @GetMapping("page")
    public PageResult page(@RequestParam("page") Integer page,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam (value="name", required=false) String name){
        //1:调用service层业务处理后的结果
       Page<Dish> dishPage =dishService.page(page,pageSize,name);
        //2:封装service层的返回结果
          //2.1:获取当前页的列表数据结果
        List<Dish> records = dishPage.getRecords();
          //2.2:获取数据的总条数
        long total = dishPage.getTotal();
        //3:返回示信友好提息
        return new PageResult(Code.OK, MessageConstant.DISH_GET_SUCCESS,records,total);


    }

    /*
    菜品的新增功能
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dish) {
        //1:调用service返回结果,对数据进行处理
        int row =dishService.save(dish);
        //2:对数据进行封装,并返回友好提示信息
        if (row <1) {
            return new Result(Code.ERROR,MessageConstant.DISH_SAVE_ERROR);
        }
        return new Result(Code.OK,MessageConstant.DISH_SAVE_SUCCESS);
    }
    /**
    * @Author yangshuai
    * @Desc: 菜品的修改功能---菜品的回显功能(先根据id去查出来)
    * @Date 2022-6-6 19:38:06
    * @Version 1.0
    **/

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id){
        //1:调用service层的数据
        DishDTO dishDTO =dishService.findById(id);
        //2:对数据进行封装
        if (dishDTO == null) {
            return new Result(Code.ERROR,MessageConstant.DISH_GET_ERROR);

        }return new Result(Code.OK,MessageConstant.DISH_GET_SUCCESS,dishDTO);
    }

    /**
    * @Author yangshuai
    * @Desc:菜品的修改功能实现
    * @Date 2022-6-6 20:50:01
    * @Version 1.0
    **/
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
         int row =dishService.update(dishDTO);
        if (row <1) {
            return new Result(Code.ERROR,MessageConstant.DISH_UPDATE_ERROR);
        }
        return new Result(Code.OK,MessageConstant.DISH_UPDATE_SUCCESS,dishDTO);
    }
    /**
    * @Author yangshuai
    * @Desc:完成停售,起售,批量停售,批量起售(由于他们单独的与批量的是一样的URL与请求方式都一样;批量中就包含单独的,所以只写批量的就可以)
    * @Date 2022-6-7 10:50:22
    * @Version 1.0
    **/
    @PostMapping("/status/{status}")
    public Result updateStatusByIds(@PathVariable("status") Integer status,
                                            @RequestParam("ids") Long[] ids ){
        //1:调用service的返回结果
        dishService.updateStatusByIds(status,ids );
        //2:对结果进行封装
        return new Result(Code.OK,MessageConstant.DISH_UPDATE_SUCCESS);
    }

    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") Long[] ids){
        //1:调用service层的封装结果
        int row=dishService.deleteByIds(ids);
        if (row < 1) {
            return new Result(Code.ERROR,MessageConstant.DISH_DELETE_ERROR);


        }return new Result(Code.OK,MessageConstant.DISH_DELETE_SUCCESS,ids);

    }
@GetMapping("/list")
    public Result findDishListByCategoryId(@RequestParam(value = "categoryId",required = false) Long categoryId,
                                           @RequestParam(value = "name",required = false) String name){
        List<Dish> dishList = dishService.findDishListByCategoryId(categoryId,name);
        if (dishList == null){
            dishList = new ArrayList<>();
        }
        return new Result(Code.OK,MessageConstant.DISH_GET_SUCCESS,dishList);	}
}

